//
//  XJSCGoodsFormat.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/6/26.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  功能:处理业务逻辑 (网络请求、事件回调)

#import "XJSCGoodsFormat.h"
#import "XJShouCangGoodsModel.h"
@interface XJSCGoodsFormat()
/**备份数据*/
@property (nonatomic, strong) NSMutableArray *backupArray;

@end

@implementation XJSCGoodsFormat

#pragma mark - 一 接口

- (void)requestSCGoodsList {
    //网络请求
    [XJHttpTool requestShouCangSuccessHandler:^(id responseObjc) {
        
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
            NSArray *collection_list = responseObjc[@"data"][@"collection_list"];
            
            NSMutableArray *tempArr = [NSMutableArray array];
            
            for (NSDictionary *dic in collection_list) {
                 XJShouCangGoodsModel *goodsM = [XJShouCangGoodsModel mj_objectWithKeyValues:dic];
                 [tempArr addObject:goodsM];
             }
            
            self.backupArray = tempArr; //备份
            
            [self.delegate requestSCGoodsListSuccessWithArray:tempArr];
            [self.delegate xjscgoodsFormatReloadDataWhenNeed];
        }
        
    } failureHandler:^(NSError *error) {
        [JRToast showWithText:[error localizedDescription]];
    }];
}


- (void)xjscgoods_selectItemAtIndexPath:(NSIndexPath *)indexPath isSelected:(BOOL)isSelected {
 
    XJShouCangGoodsModel *model = self.backupArray[indexPath.row];
    model.isScgoosModelSelected = isSelected;
}

/**全选、取消全选*/
- (void)xjscgoods_selecteaAllWithState:(BOOL)state {
    for (XJShouCangGoodsModel *model in self.backupArray) {
        model.isScgoosModelSelected = state;
    }
    
    [self.delegate xjscgoodsFormatReloadDataWhenNeed];
}
/**批量删除:收藏的商品*/
- (void)beginToDeleteSelectedGoods {
    
    NSMutableArray *tempArr = [NSMutableArray array];
    
    for (XJShouCangGoodsModel *model in self.backupArray) {
        if (model.isScgoosModelSelected) {
            [tempArr addObject:model];
        }
    }
    
    [self.delegate xjscgoodsFormatWillDeleteSelectedTopics:tempArr];
    [self.delegate xjscgoodsFormatReloadDataWhenNeed];
}

/**注意调用时机*/
- (void)deleteSelectedGoodsArr:(NSArray *)array {
    XJLog(@"编辑收藏的商品 - 要删除的商品个数:%ld",array.count);
    
    NSString *parmeter = [self assembleRequestParameterWithArr:array];
    
    //调删除接口
    [XJHttpTool requestDeleteShouCangWithIds:parmeter successH:^(id responseObjc) {
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
            [self requestSCGoodsList]; //商家总
        }
    } failureH:^(NSError *error) {
        [JRToast showWithText:[error localizedDescription]];
    }];
}

#pragma mark - Private method
//组装请求参数
- (NSString *)assembleRequestParameterWithArr:(NSArray *)arr {
    
    NSMutableString *tempStr = @"".mutableCopy;
    
    for (XJShouCangGoodsModel *model in arr) {
        
        
        if (model.isScgoosModelSelected) {
            NSString *goodId = [NSString stringWithFormat:@"%@",model.goodsId];
            
            [tempStr appendFormat:@"%@,",goodId];
        }
    }
    
    return [tempStr copy];
}



@end
